<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\Goods;
use api\wxapp\model\GoodsActivity;
use api\wxapp\model\OrderList;
use api\wxapp\model\Order;
use api\wxapp\model\Comment;
class SeckillController extends ApiBaseController
{

    public function index()
    {
        $params = $this->getParam();
        $this->checkshopid($params);
        //查询正在进行的秒杀活动
        $where[] = ['stime','elt',date('Y-m-d H:i:s',time())];
        $where[] = ['etime','egt',date('Y-m-d H:i:s',time())];
        $where[] = ['shop_id','eq',$params['shop_id']];
        $list = GoodsActivity::where($where)->all()->each(function($item, $key){
            $item['value'] = json_decode($item['value'],true);
            if($item['shop_id']){
                $item['lable'] = '自提';
            }else{
                $item['lable'] = '直邮';
            }
            $goods = array();
            $goodsarr = array();
            foreach($item['value'] as $k=>$v){
                $goods['goods_id'] = $k;
                $find = Goods::get($k);
                $goods['name'] = $find['name'];
                $goods['img'] = $find['img'];
                $goods['total'] = OrderList::where('is_activity',$item['id'])->SUM('num');
                $goods['price'] = $v;
                $goods['old_price'] = ShopGoods::where(['goods_id'=>$k,'shop_id'=>$item['shop_id']])->value('price');
                array_push($goodsarr,$goods);
            }
            $item['goods'] = $goodsarr;
            return $item;
        });
        //查询 上一个结束的活动
        $map[] = ['etime','elt',date('Y-m-d H:i:s',time())];
        $pass = GoodsActivity::where($map)->order('etime desc')->value('stime');
        $map1[] = ['stime','egt',date('Y-m-d H:i:s',time())];
        $soon = GoodsActivity::where($map1)->order('stime')->value('stime');

        $this->success('请求成功!',['pass'=>$pass,'now'=>$list,'soon'=>$soon]);
    }
    public function time()
    {
        $params = $this->getParam();
        $this->checkshopid($params);
        $where[] = ['shop_id','eq',$params['shop_id']];
        $date = date('Y-m-d H:i:s',time());
        $times = GoodsActivity::where($where)->order('stime')->field('stime,etime')->select();
        foreach($times as $k=>$v){
            if($v['stime'] <= $date && $date <= $v['etime']){
                //正在进行
                $times[$k]['res'] = true;
            }else{
                $times[$k]['res'] = false;
            }
        };
        $this->success('请求成功!',$times);

    }
    public function info()
    {
        $params = $this->getParam();
        if(!isset($params['activity_id'])){
            $this->error('参数缺失!!!!!');
        }
        $this->checkgoodsId($params);
        $info = GoodsActivity::get($params['activity_id']);
        $goods = Goods::get($params['goods_id']);
        $params['stime'] = $info['stime'];
        $params['name'] = $goods['name'];
        $params['img'] = $goods['img'];
        $params['content'] = $goods['content'];
        $value = json_decode($info['value'],true);
        $params['price'] = $value[$params['goods_id']];
        $params['old_price'] = ShopGoods::where(['goods_id'=>$params['goods_id'],'shop_id'=>$info['shop_id']])->value('price');

        $map[] = ['is_activity','eq',$params['activity_id']];
        $params['count'] = OrderList::where($map)->SUM('num');
        $params['commment'] = Comment::where('goods_id',$params['goods_id'])->count();
        $this->success('请求成功!',$params);
    }
    //秒杀商城立即抢购
    public function buy()
    {
        $params = $this->getParam();
        if(!isset($params['activity_id'])){
            $this->error('参数缺失!!!!!');
        }
        !isset($params['pay_type']) ? $this->error('参数缺失!!!') : '';
        $this->checkuid($params);
        $this->checkshopid($params);
        $this->checkgoodsId($params);

        $insert['uid'] = $params['uid']; //用户id
        if(isset($params['freight'])){
            $insert['freight'] = $params['freight'];
        }else{
            $insert['freight'] = 0;
        }
        $insert['pay_price'] = $params['pay_price'];//支付价格
        $insert['status'] = 1; //未支付状态
        $insert['addtime'] = date('Y-m-d H:i:s',time()); //未支付状态
        $insert['pay_type'] = $params['pay_type']; //支付方式
        $insert['shop_id'] = $params['shop_id'];
        $insert['type'] = 3;//1:常规订单2:积分订单3:秒杀订单
        if(isset($params['is_coupon'])){
            $insert['is_coupon'] = $params['is_coupon']; //是否使用了优惠券
        }
        if(isset($params['remark'])){
            $insert['remark'] = $params['remark']; //订单备注
        }
        $insert['ordernum'] = $this->getOrderNum($params['uid']);

        !isset($params['num']) ? $params['num'] = 1 : '';

        $insert['old_price'] = $params['old_price'];
        $insert['remark'] = '秒杀商城抢购';
        $insert['goods_price'] = $params['pay_price'];
        Db::startTrans();
        try{
            $res = Order::insertGetId($insert);
            if(!$res){
                throw new \Exception('订单记录表储存失败');
            }
            //
            $data['goods_id'] = $params['goods_id'];
            $data['num'] = $params['num'];
            $data['addtime'] = date('Y-m-d H:i:s',time());
            $data['is_activity'] = $params['activity_id'];
            $data['price'] = $params['pay_price'];
            $data['old_price'] = $params['old_price'];
            $data['order_id'] = $res;
            $res1 = OrderList::insert($data);
            if(!$res1){
                throw new \Exception('订单记录详情表储存失败');
            }
            //根据支付金额  赠送积分  等支付成功 之后在赠送
            Db::commit(); 
        }catch(\Exception $e){
            Db::rollback(); //回滚事务
            $this->error($e->getMessage());
        }
        $this->success('订单提交成功!',true);
    }
}
